package servlets;
import dataManagers.*;
import entity.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class authenticationServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");


        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        Users users=UserDM.retrieve(username);
        HttpSession session = request.getSession(true);
        RequestDispatcher dispatcher;
        
        
        if(users!=null){
            if(password.equals(users.getPassword())){
                session.setAttribute("user",users);
                if(users.getPriv()==0){
                	response.sendRedirect("musicData.jsp");
                }else{
                	response.sendRedirect("home.jsp");
                }
            }
        }else{
            String errorMsg="";
            /*if(username.equals("")&&password.equals("")){
                errorMsg="Please enter your username and password";
            }else if(username.equals("")){
                errorMsg="Please enter your username";
            }
            else if(password.equals("")){
                errorMsg="Please enter your password";
            }else{*/
                errorMsg="Invalid username or password";
          //  }
        request.setAttribute("errorMsg", errorMsg);
        dispatcher = request.getRequestDispatcher("index.jsp");
        dispatcher.forward(request, response);
    }

  }
}